package engine.render;

import engine.res.Bitmap;
import engine.res.Color;
import engine.res.Res;
import math.Maths;
import math.vector.vec3f;

public class GLManager 
{
    
    private vec3f rotations; // { altitude, azimuth, roll }
    public vec3f getRotations() 
    {
        return rotations;
    }
    
    private vec3f translation;
    public vec3f getTranslation() 
    {
        return translation;
    }
    
    private Color currentColor;
    public Color getColor() {
        return currentColor;
    }
    
    private Shader currentShader;
    public Shader getShader() 
    {
        return currentShader;
    }
    
    private Bitmap currentTex;
    public Bitmap getTex() {
        return currentTex;
    }
    
    public GLManager() 
    {
        rotations = new vec3f();
        translation = new vec3f();
        currentColor = null;
        currentShader = null;
        currentTex = null;
    }
    
    //<editor-fold defaultstate="collapsed" desc="bindBitmap">
    public void bindBitmap(String bitmapName) 
    {
        Bitmap desired = Res.loader().getTex(bitmapName);
        if (!desired.equals(currentTex)) 
        {
            currentTex = desired;
            currentTex.bind();
        }
    }
    
    public void bindBitmap(Bitmap desired) 
    {
        if (!desired.equals(currentTex)) 
        {
            currentTex = desired;
            currentTex.bind();
        }
    }
    //</editor-fold>
    
    //<editor-fold defaultstate="collapsed" desc="bindShader">
    public void bindShader(Shader desired) 
    {
        if (!desired.equals(currentShader)) 
        {
            if (currentShader != null) 
            {
                currentShader.release();
            }
            currentShader = desired;
            currentShader.bind();
        }
    }
    
    public void bindShader(String shaderName) 
    {
        Shader desired = Res.loader().getShader(shaderName);
        bindShader(desired);
    }
    //</editor-fold>
    
    //<editor-fold defaultstate="collapsed" desc="bindColor">
    public void bindColor(Color col) 
    {
        if (col != null) 
        {
            if (!col.equals(currentColor)) 
            {
                currentColor = col;
                currentColor.bind();
            }
        }
    }
    //</editor-fold>
    
    //<editor-fold defaultstate="collapsed" desc="bindRotation">
    public void bindRotation(float altitudeRad, float azimuthRad, float rollRad) 
    {
        rotations.setTo(altitudeRad, azimuthRad, rollRad);
    }
    
    public void bindRotation(vec3f _rotations) 
    {
        rotations.setTo(_rotations);
    }
    //</editor-fold>
    
    //<editor-fold defaultstate="collapsed" desc="bindTranslation">
    public void bindTranslation(float dx, float dy, float dz) 
    {
        translation.setTo(dx, dy, dz);
    }
    
    public void bindTranslation(vec3f _translation) 
    {
        translation.setTo(_translation);
    }
    //</editor-fold>
    
    //<editor-fold defaultstate="collapsed" desc="getCamDirs">
    public vec3f getCamDir() 
    {
        vec3f dirLook = new vec3f();
        float cosRotsX = Maths.cosRad(rotations.x);
        dirLook.x = Maths.sinRad(rotations.y) * cosRotsX;
        dirLook.y = Maths.sinRad(rotations.x);
        dirLook.z = -1.0f * Maths.cosRad(rotations.y) * cosRotsX;
        return dirLook;
    }
    
    public vec3f getCamDirRight() 
    {
        vec3f dirLookRght = new vec3f();
        dirLookRght.x = Maths.cosRad(rotations.y);
        //        dirLookRght.y = 0.0f;
        dirLookRght.z = Maths.sinRad(rotations.y);
        return dirLookRght;
    }
    
    public vec3f getCamDirUp() 
    {
        vec3f dirLookUp = new vec3f();
        float cosRotsX = Maths.cosRad(rotations.x);
        float sinRotsX = Maths.sinRad(rotations.x);
        float cosRotsY = Maths.cosRad(rotations.y);
        float sinRotsY = Maths.sinRad(rotations.y);
        dirLookUp.x = -1.0f * sinRotsX * sinRotsY;
        dirLookUp.y = cosRotsX * (sinRotsY * sinRotsY + cosRotsY * cosRotsY);
        dirLookUp.z = sinRotsX * cosRotsY;
        return dirLookUp;
    }
    //</editor-fold>
    
}
